<?php
/**
 * 消息管理模型.
 * User: wuhao
 * Date: 2017/4/17
 * Time: 17:04
 */
namespace app\backend\model;

use think\Model;

class MessageTemplate extends Model
{
    private $_messageTemplate = 'z_message_template';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增消息模板
     * @date   2017-04-17
     * @author wuhao
     *
     * @param  int     $memberId      新增人id
     * @param  string  $title         消息标题
     * @param  int     $contractType  合同类型
     * @param  int     $eventType     通知事件
     * @param  int     $messageType   消息类型
     * @param  int     $member        通知对象
     * @param  int     $sendType      发送对象
     * @param  int     $notifyMethod  通知方式
     * @param  int     $notifyDay     提前通知天数
     * @param  string  $content       消息内容
     * @return bool
     */
    public function addTemplate($memberId, $title, $contractType, $eventType, $messageType, $member, $sendType, $notifyMethod, $notifyDay, $content)
    {
        if (!$memberId || !$title || !$contractType || !$eventType || !$messageType || !$member ||!$sendType || !$notifyMethod || !$notifyDay || !$content) {
            return false;
        }
        $data = [
            'title'         => $title,
            'contract_type' => $contractType,
            'event_type'    => $eventType,
            'message_type'  => $messageType,
            'member'        => $member,
            'send_type'     => $sendType,
            'notify_method' => $notifyMethod,
            'notify_day'    => $notifyDay,
            'content'       => $content,
            'create_time'   => time(),
            'update_time'   => time(),
            'create_user'   => $memberId,
        ];
        return $this->setTable($this->_messageTemplate)->insertGetId($data);
    }

    /**
     * 检测通知事件唯一性
     * @date   2017-04-18
     * @author wuhao
     *
     * @param  int     $contractType  合同类型
     * @param  int     $eventType     通知事件
     * @param  int     $member        通知对象
     * @return array
     */
    public function checkEvent($contractType, $eventType, $member)
    {
        if (!$contractType || !$eventType || !$member) {
            return false;
        }
        $where = [
            'contract_type' => $contractType,
            'event_type'    => $eventType,
            'member'        => $member,
        ];
        $res = $this->setTable($this->_messageTemplate)->field('id, title, content, notify_day, notify_method, send_type')->where($where)->find();
        return $res ?: [];
    }

    /**
     * 检测消息类型唯一性
     * @date   2017-04-18
     * @author wuhao
     *
     * @param  int     $contractType  合同类型
     * @param  int     $messageType   消息类型
     * @param  int     $member        通知对象
     * @return array
     */
    public function checkMessage($contractType, $messageType, $member)
    {
        if (!$contractType ||  !$messageType || !$member) {
            return false;
        }
        $where = [
            'contract_type' => $contractType,
            'message_type'  => $messageType,
            'member'        => $member,
        ];
        $res = $this->setTable($this->_messageTemplate)->field('id, title, content, notify_day, notify_method, send_type')->where($where)->find();
        return $res ?: [];
    }

    /**
     * 查询已有模板列表
     * @date   2017-04-17
     * @author wuhao
     *
     * @param  string  $title         消息标题
     * @param  int     $eventType     通知事件
     * @param  int     $messageType   消息类型
     * @param  int     $page          页数
     * @param  int     $pageSize      每页个数
     * @return array
     */
    public function searchTemplate($title, $eventType, $messageType, $page, $pageSize)
    {
        if (!$page || !$pageSize) {
            return false;
        }
        $where = [];
        if ($title) {
            $where['title'] = ['like', "%$title%"];
        }
        if ($eventType) {
            $where['event_type'] = $eventType;
        }
        if ($messageType) {
            $where['message_type'] = $messageType;
        }
        $res = $this->setTable($this->_messageTemplate)
            ->field('id, title, message_type, event_type, send_type, member, notify_method')
            ->where($where)
            ->page($page, $pageSize)
            ->select();
        if (!$res) {
            return [];
        }

        $sum = $this->setTable($this->_messageTemplate)->where($where)->count();
        $data  = [
            'list'         => $res,
            'total'        => ceil($sum / $pageSize),
            'current_page' => $page,
        ];
        return $data;
    }

    /**
     * 删除模板
     * @date   2017-04-17
     * @author wuhao
     *
     * @param  int     $id     模板id
     * @return array
     */
    public function deleteTemplate($id)
    {
        if (!$id || !is_numeric($id)) {
            return false;
        }
        $where = ['id' => $id];
        return $this->setTable($this->_messageTemplate)->where($where)->delete();
    }

    /**
     * 根据模板id获取模板详细信息
     * @date   2017-04-17
     * @author wuhao
     *
     * @param  int     $id     模板id
     * @return array
     */
    public function getTemplateById($id)
    {
        if (!$id || !is_numeric($id)) {
            return false;
        }
        $where = ['id' => $id];
        return $this->setTable($this->_messageTemplate)->field('*')->where($where)->find();
    }

    /**
     * 编辑模板信息
     * @date   2017-04-17
     * @author wuhao
     *
     * @param  int     $id            模板id
     * @param  string  $title         消息标题
     * @param  int     $sendType      发送对象
     * @param  int     $notifyMethod  通知方式
     * @param  int     $notifyDay     提前通知天数
     * @param  string  $content       消息内容
     * @return bool
     */
    public function editTemplate($id, $title,  $sendType, $notifyMethod, $notifyDay, $content)
    {
        if (!$id || !is_numeric($id) || !$title  ||!$sendType || !$notifyMethod || !$notifyDay || !$content) {
            return false;
        }
        $where = ['id' => $id];
        $data  = [
            'title'         => $title,
            'send_type'     => $sendType,
            'notify_method' => $notifyMethod,
            'notify_day'    => $notifyDay,
            'content'       => $content,
            'update_time'   => time(),
        ];
        return $this->setTable($this->_messageTemplate)->where($where)->update($data);
    }

    /**
     * 根据催缴或者到账类型获取对应的模板信息
     * @date   2017-04-17
     * @author wuhao
     *
     * @param  int     $contractType  合同类型 1=租赁 2=托管
     * @param  int     $eventType     消息类型 1=催缴 2=到账 3=到期
     * @param  int     $member        通知对象 0=所有 1=付款方 2=收款方
     * @return array
     */
    public function getTemplateByPayOrIncome($contractType, $eventType, $member = 0)
    {
        if (!in_array($contractType, [1, 2]) || !in_array($eventType, [1, 2, 3]) || !in_array($member, [0, 1, 2])) {
            return false;
        }
        if (in_array($member, [1, 2])) {
            $where['member'] = $member;
        }
        $where['contract_type'] = $contractType;
        $where['event_type']    = $eventType;
        $res = $this->setTable($this->_messageTemplate)->field('*')->where($where)->select();
        return $res ?: [];
    }
}